package HashMapSeries;

import java.util.HashSet;
import java.util.Set;

public class IsHappySolution_202 {
    public boolean isHappy(int n) {
        Set<Integer> record = new HashSet<>();

        while (n != 1 && !record.contains(n)) {
            record.add(n);
            n = sumNumber(n);
        }

        return n == 1;
    }

    //求每个数字平方和
    private int sumNumber(int n) {
        int sum = 0;

        while (n > 0) {
            sum += ((n % 10) * (n % 10));
            n = n / 10;
        }

        return sum;
    }
}
